/**
 * Created by cuishifeng on 2019/2/12.
 */

/*
 * md parse js
 */
function toHtml(md) {
    return marked(md);
}

function highlightBlock() {
    document.querySelectorAll('pre code').forEach((block) => {
        hljs.highlightBlock(block);
    });
}

let rendererMD = new marked.Renderer();
marked.setOptions({
    renderer: rendererMD,
    gfm: true,
    tables: true,
    breaks: false,
    pedantic: false,
    sanitize: false,
    smartLists: true,
    smartypants: false,
    emoji: true
});
marked.setOptions({
    highlight: function (code) {
        return hljs.highlightAuto(code).value;
    },
});

// 浮动导航显隐
function hide(ele) {
    removeClass(getId(ele), "d-block")
    addClass(getId(ele), "d-none")
}

function myShow(ele) {
    removeClass(getId(ele), "d-none")
    addClass(getId(ele), "d-block")
}

// 添加class
function addClass(obj, cls) {
    const obj_class = obj.className, blank = (obj_class != "") ? " " : "";
    let a = obj_class + blank + cls;
    obj.className = a
}

// 删除class
function removeClass(obj, cls) {
    let obj_class = " " + obj.className + " ";
    obj_class = obj_class.replace(/(\s+)/gi, " "), r = obj_class.replace(" " + cls + " ", " ");
    r = r.replace(/(^\s+)|(\s+$)/g, "");
    obj.className = r
};

// 获取Id class对象
function getId(id) {
    return document.getElementById(id);
}

function getClass(cls) {
    return document.getElementsByClassName(cls);
}

// 回到页面底部
function goBottom() {
    window.scrollTo(0, document.documentElement.clientHeight);
}

function requestPost(url, data) {
    return requestApi(url, "POST", data, "application/x-www-form-urlencoded;charset=utf-8")
}

function requestGet(url, data) {
    return requestApi(url, "GET", data, "application/x-www-form-urlencoded;charset=utf-8")
}

let issuePreFix = "issue:";
let userPreFix = "user:";
let commentsPreFix = "comments:";
let token = "41f9a4419757978cb227c51d45e4b201";


function requestGetIssue(owner, repo, number) {
    let url = "https://gitee.com/api/v5/repos/" + owner + "/" + repo + "/issues/" + number;
    if (!lscache.supported()) {
        return requestApi(url, "GET", {"access_token": token}, "application/x-www-form-urlencoded;charset=utf-8")
    } else {
        let data = lscache.get(issuePreFix + owner + repo + number);
        if (data == null) {
            data = requestApi(url, "GET", {"access_token": token}, "application/x-www-form-urlencoded;charset=utf-8")
            cacheIssue(owner, repo, number, data);
        }
        return data;
    }
}

function requestGetIssueComments(owner, repo, number, page) {
    let url = "https://gitee.com/api/v5/repos/" + owner + "/" + repo + "/issues/" + number + "/comments";
    if (!lscache.supported()) {
        return requestApi(url, "GET",
            {"access_token": token, "page": page, "per_page": 50, "order": "asc"},
            "application/x-www-form-urlencoded;charset=utf-8")
    } else {
        let data = lscache.get(commentsPreFix + owner + repo + number);
        if (data == null) {
            data = requestApi(url, "GET",
                {"access_token": token, "page": page, "per_page": 50, "order": "asc"},
                "application/x-www-form-urlencoded;charset=utf-8")
            cacheComments(owner, repo, number, data);
        }
        return data;
    }

}

function requestGetUser() {
    let url = "https://gitee.com/api/v5/user";
    if (!lscache.supported()) {
        return requestApi(url, "GET", {"access_token": token}, "application/x-www-form-urlencoded;charset=utf-8")
    }else {
        let data = lscache.get(userPreFix + owner);
        if (data == null){
            let data = requestApi(url, "GET", {"access_token": token}, "application/x-www-form-urlencoded;charset=utf-8")
            cacheUser(owner,data);
        }
        return data;
    }
}

function cacheIssue(owner, repo, number, data) {
    if (data != null) {
        lscache.set(issuePreFix + owner + repo + number, data, 60);
        lscache.flushExpired();
    }
}

function cacheComments(owner, repo, number, data) {
    if (data != null) {
        lscache.set(commentsPreFix + owner + repo + number, data, 60);
        lscache.flushExpired();
    }
}

function cacheUser(owner, data) {
    if (data != null) {
        lscache.set(userPreFix + owner, data, 1000);
        lscache.flushExpired();
    }
}
function flushCache() {
    lscache.flush();
}

// 公共请求
function requestApi(url, type, data, contentType) {
    $.ajax({
        type: type,
        url: url,
        contentType: contentType,
        data: data,
        dataType: 'json',
        async: false,
        success: function (data) {
            result = data;
        }
    });
    return result;
}

function requestImg(url, id) {
    let obj = new FormData();
    let file = document.getElementById(id).files[0];
    obj.append("file", file);
    let result = "";
    $.ajax({
        url: url,
        type: 'POST',
        data: obj,
        contentType: false,
        processData: false,
        mimeType: 'multipart/form-data',
        async: false,
        success: function (data) {
            result = data;
        }
    })
    return result;
}
